﻿using System;
using StockholmsStad.Utilities.Enums;
using System.Diagnostics;
using System.ServiceModel;

namespace StockholmsStad.Utilities.Exceptions
{
    public class ExceptionManager
    {
        private const string EVENTLOG_SOURCE = "Parvis";

        public static void WriteToEventLog(string message, EventLogEntryType eventLogEntryType)
        {
            EventLog.WriteEntry(EVENTLOG_SOURCE, message, eventLogEntryType);
        }

        public static void WriteToEventLog(Exception e, EventLogEntryType eventLogEntryType)
        {
            string error = GetDetailedExceptionInformationRecursive(e);
            EventLog.WriteEntry(EVENTLOG_SOURCE, error, eventLogEntryType);
        }

        public static string TranslateErrorCode(ApplicationErrorCode errorCode)
        {
            return EnumUtility.GetEnumDescription(errorCode);
        }

        public static string TranslateErrorCode(SystemErrorCode errorCode)
        {
            return EnumUtility.GetEnumDescription(errorCode);
        }

        public static string GetDetailedExceptionInformationRecursive(Exception ex)
        {
            string errorMessage = String.Empty;

            Exception innerException = ex;
            while (innerException != null)
            {
                if (innerException is StockholmStadApplicationException)
                {
                    var exception = (StockholmStadApplicationException)innerException;
                    errorMessage += TranslateErrorCode(exception.ErrorCode) + "\n\n" + exception.StackTrace;
                }
                if (innerException is StockholmStadSystemException)
                {
                    var exception = (StockholmStadSystemException)innerException;
                    errorMessage += TranslateErrorCode(exception.ErrorCode) + "\n\n" + exception.StackTrace;
                }
                else if (innerException is FaultException<ExceptionDetail>)
                {
                    var faultException = (FaultException<ExceptionDetail>)innerException;
                    errorMessage += faultException.Detail.Message + "\n\n" + faultException.Detail.StackTrace;
                }
                else if (innerException is FaultException)
                {
                    var faultException = (FaultException)innerException;
                    errorMessage += faultException.Message + "\n\n" + faultException.StackTrace;
                }
                else if (innerException is CommunicationException)
                {
                    var commException = (CommunicationException)innerException;
                    errorMessage += commException.Message + "\n\n" + commException.StackTrace;
                }
                else
                {
                    errorMessage += innerException.Message + "\n\n" + innerException.StackTrace;
                }

                errorMessage += "\n\n\n\n";
                innerException = innerException.InnerException;
            }

            return errorMessage;
        }
    }
}
